<?php


if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class m_penyakit extends CI_Model {


	function ambil() {
		$ambil = $this->db->get('penyakit');
		if ($ambil->num_rows() > 0) {
			foreach ($ambil->result() as $data) {
				$hasil[] = $data;
			}
			return $hasil;
		}
	}


	function ambilsemuagejala() {

		$ambil = $this->db->get('gejala');
		if ($ambil->num_rows() > 0) {
			foreach ($ambil->result() as $data) {
				$hasil[] = $data;
			}
			return $hasil;
		}
	}




	function tambah(){
		$kode = $this->input->post('kode');
		$nama = $this->input->post('nama');
		$data1 = array('kd_penyakit' => $kode,'penyakit' => $nama);
		if($this->cek_penyakit($kode) == false) {
			$this->db->trans_start();
			$this->db->insert('penyakit', $data1);
			$this->db->trans_complete();
			$this->session->set_flashdata('result_pass_true', 'Data Penyakit berhasil ditambah');
			header('location:' . site_url('dashboard/edit_profil'));
		} else{
			$this->session->set_flashdata('result_pass', 'Kode Penyakit sudah pernah digunakan');
			header('location:' . site_url('dashboard/edit_profil'));
		}
	}

	function cek_gejala($kode=""){
		$ambil = $this->db->get_where('gejala', array('kd_gejala' => $kode));
		if($ambil->num_rows > 0){
			return true;
		}else{
			return false;
		}
	}

	function cek_penyakit($kode=""){
		$ambil = $this->db->get_where('penyakit', array('kd_penyakit' => $kode));
		if($ambil->num_rows > 0){
			return true;
		}else{
			return false;
		}
	}

	function cek_penyakit_diagnosis($kode='')
	{
		$ambil = $this->db->get_where('bobot_gejala', array('id_penyakit' => $kode));
		if($ambil->num_rows > 0){
			return true;
		}else{
			return false;
		}
	}


	function ambilbyid($id="") {
		$ambil = $this->db->get_where('penyakit', array('id_penyakit' => $id));
		return $ambil;
	}


	function update($id="")
	{
		$kode = $this->input->post('kode');
		$nama = $this->input->post('nama');
		$data1 = array('kd_penyakit' => $kode,'penyakit' => $nama);
		if($this->cek_penyakit($kode) == false) {
			$this->db->trans_start();
			$this->db->where('id_penyakit', $id);
			$this->db->update('penyakit', $data1);
			$this->db->trans_complete();
			$this->session->set_flashdata('result_pass_true', 'Data Penyakit berhasil diperbarui');
		} else{
			$this->session->set_flashdata('result_pass', 'Kode Penyakit sudah pernah digunakan');
			header('location:' . site_url('dashboard/edit_profil'));
		}
		header('location:' . site_url('penyakit/edit'.$id));

	}	

	function ambil_gejala($id_penyakit, $jenis) {
		if($jenis == 'sapi'){
			$jenishewan = '1';
		}else if($jenis == 'kambing' ){
			$jenishewan = '2';
		}else{
			$jenishewan = '3';
		}

		$this->db->select('*');
		$this->db->from('gejala');
		$this->db->join('bobot_gejala', 'gejala.id_gejala=bobot_gejala.id_gejala');
		$this->db->where('bobot_gejala.id_penyakit', $id_penyakit);
		$this->db->where('bobot_gejala.jenishewan', $jenishewan);
		$ambil = $this->db->get();
		if ($ambil->num_rows() > 0) {
			foreach ($ambil->result() as $data) {
				$hasil[] = $data;
			}
			return $hasil;
		}
	}


	function ambil_all_gejala(){

		$ambil = $this->db->get('gejala');
		if ($ambil->num_rows() > 0) {
			foreach ($ambil->result() as $data) {
				$hasil[] = $data;
			}
			return $hasil;
		}
	}


	function hapus($a) {
		if($this->cek_penyakit_diagnosis($a) == false) {
			$this->db->trans_start();
			$this->db->delete('penyakit', array('id_penyakit' => $a));
			$this->db->trans_complete();
			return;
			$this->session->set_flashdata('result_pass_true', 'Data Penyakit berhasil dihapus');
		} else{
			$this->session->set_flashdata('result_pass', 'Kode Penyakit masih digunakan');
			header('location:' . site_url('penyakit'));
		}
	}

	function tambah_gejala(){
		$kode = $this->input->post('kode');
		$nama = $this->input->post('nama');
		$id_penyakit = $this->input->post('id');
		$data1 = array('kd_gejala' => $kode,'gejala' => $nama);
		
		if($this->cek_gejala($kode) == false) {
			$this->db->trans_start();
			$this->db->insert('gejala', $data1);
			$this->db->trans_complete();
			$this->session->set_flashdata('result_pass_true', 'Data Gejala berhasil ditambah');
			header('location:' . site_url('dashboard/tambah_gejala').$id_penyakit);
		} else{
			$this->session->set_flashdata('result_pass', 'Kode Gejala sudah pernah digunakan');
			header('location:' .site_url('dashboard/tambah_gejala').$id_penyakit);
		}
	}


	function update_gejala($id="")
	{
		$kode = $this->input->post('kode');
		$nama = $this->input->post('nama');
		$data1 = array('kd_gejala' => $kode,'gejala' => $nama);
		if($this->cek_gejala($kode) == false) {
			$this->db->trans_start();
			$this->db->where('id_gejala', $id);
			$this->db->update('gejala', $data1);
			$this->db->trans_complete();
			$this->session->set_flashdata('result_pass_true', 'Data Gejala berhasil diperbarui');
		} else{
			$this->session->set_flashdata('result_pass', 'Kode Gejala sudah pernah digunakan');
			header('location:' .site_url('dashboard/tambah_gejala').$id_penyakit);
		}
		header('location:' . site_url('penyakit/edit_gejala/'.$id));

	}

	function cek_bobot($id_gejala){
		$ambil = $this->db->get_where('bobot_gejala', array('id_gejala' => $id_gejala));
		if($ambil->num_rows > 0){
			return true;
		}else{
			return false;
		}
	}

	function hapus_gejala($id_gejala){
		$this->db->trans_start();
		$this->db->delete('bobot_gejala', array('id_gejala' => $id_gejala));
		$this->db->delete('gejala', array('id_gejala' => $id_gejala));
		$this->db->trans_complete();
		return;
	}

	function ambilgejalabyid($id="") {
		$ambil = $this->db->get_where('gejala', array('id_gejala' => $id));
		return $ambil;
	}



	function get_nama_penyakit($id)
	{
		$this->db->select('penyakit');
		$data =  $this->db->get_where('penyakit', array('id_penyakit' => $id))->row();
		return $data->penyakit;
	}


	function getallpenyakit (){
		$this->db->select('*');
		$this->db->from('penyakit');
//		$this->db->join('bobot_gejala', 'gejala.id_gejala=bobot_gejala.id_gejala');
//		$this->db->where('bobot_gejala.jenishewan', $jenis);
		$ambil = $this->db->get();
		if ($ambil->num_rows() > 0) {
			foreach ($ambil->result() as $data) {
				$hasil[] = $data;
			}
			return $hasil;
		}
	}

	



	function get_json()
	{
		$sql = 'SELECT gejala FROM gejala';
		$ambil= $this->db->query($sql);
		foreach ($ambil->result() as $row)
		{
			$hasil[] = $row->gejala;
		}
		return $hasil;


	}

	function penyakit_wheregejala($id, $hewan){
		$this->db->select('*');
		$this->db->from('penyakit');
		$this->db->join('bobot_gejala', 'penyakit.id_penyakit=bobot_gejala.id_penyakit');
		$this->db->where('bobot_gejala.id_gejala', $id);
		$this->db->where('bobot_gejala.jenishewan', $hewan);
		$ambil = $this->db->get();
		if ($ambil->num_rows() > 0) {
			foreach ($ambil->result() as $data) {
				$hasil[] = $data;
			}
			return $hasil;
		}
	}

	function get_user($username){
		$this->db->select('id_user');
		$this->db->from('user');
		$this->db->where('username', $username);
		$ambil = $this->db->get();
		if ($ambil->num_rows() > 0) {
			foreach ($ambil->result() as $data) {
				$hasil[] = $data;
			}
			return $hasil;
		}
	}





}